const http = require('http')
const url = require('url')
const path = require('path')
const fs = require('fs')
const mime=require('mime')
let application = http.createServer()
application.on('request', (request, response) => {
    console.log(request.url);
    let { pathname } = url.parse(request.url)
    console.log(pathname);// /home
    pathname = pathname === '/' ? 'index1.html' : pathname
    console.log(__dirname);//D:\work\jianGuoYun\node_practice
    let staticPath = path.join(__dirname, 'static', 'public', pathname)
    // console.log(staticPath);//D:\work\jianGuoYun\node_practice\static\public\index1.html
    // console.log(mime.getType(staticPath));
    let staticFileType=mime.getType(staticPath)
    fs.readFile(staticPath, (error, data) => {
        if (error !== null) {
            console.log(error);
            response.writeHead(404, {
                'content-type': 'text/html;charset=utf8'
            })
            response.end('文件读取失败了！')
            return
        }
        response.writeHead(200,{
            'content-type':staticFileType+';charset=utf8'
        })
        response.end(data)
    })

})
application.listen(3006)
console.log("服务器已经启动，请访问：http://127.0.0.1:3006");